<?php
/**
 * @package Autogenerator for Joomla!
 * @version 3.0.0
 * @author Truong Cong Duc <truongcongduc4@gmail.com>
 * @link autogenerator.blogspot.com
 * @copyright (c) 2013, Truong Cong Duc
 * @license GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

JHtml::addIncludePath(JPATH_COMPONENT . '/helpers/html');
JHtml::_('bootstrap.tooltip');
JHtml::_('behavior.multiselect');
JHtml::_('dropdown.init');
JHtml::_('formbehavior.chosen', 'select');

$user = JFactory::getUser();
$userId = $user->get('id');
$listOrder = $this->escape($this->state->get('list.ordering'));
$listDirn = $this->escape($this->state->get('list.direction'));
$saveOrder = $listOrder == 'af.ordering';
if ($saveOrder) {
    $saveOrderingUrl = 'index.php?option=com_autogenerator&task=fields.saveOrderAjax&tmpl=component';
    JHtml::_('sortablelist.sortable', 'fieldsList', 'adminForm', strtolower($listDirn), $saveOrderingUrl);
}
?>
<link rel="stylesheet" href="<?php echo JUri::root() . AUTOGENERATOR_URL_CSS . 'autogenerator.css'; ?>" type="text/css">
<script src="<?php echo JUri::root() . AUTOGENERATOR_URL_JS . 'autogenerator.js'; ?>" type="text/javascript"></script>

<script type="text/javascript">
    jQuery(document).ready(function() {
        Autogenerator.Change_Status.init();
        Autogenerator.Select_Inline.init();
    });
    
    Joomla.orderTable = function() {
        table = document.getElementById("sortTable");
        direction = document.getElementById("directionTable");
        order = table.options[table.selectedIndex].value;
        if (order != '<?php echo $listOrder; ?>') {
            dirn = 'asc';
        } else {
            dirn = direction.options[direction.selectedIndex].value;
        }
        Joomla.tableOrdering(order, dirn, '');
    }
</script>

<form action="<?php echo JRoute::_('index.php?option=com_autogenerator&view=fields'); ?>" method="post" name="adminForm" id="adminForm">
    <?php if (!empty($this->sidebar)): ?>
        <div id="j-sidebar-container" class="span2">
            <?php echo $this->sidebar; ?>
        </div>

        <div id="j-main-container" class="span10">
    <?php else : ?>
        <div id="j-main-container">
    <?php endif; ?>
            <div id="filter-bar" class="btn-toolbar">
                <div class="filter-search btn-group pull-left">
                    <label for="filter_search" class="element-invisible"><?php echo JText::_('COM_AUTOGENERATOR_FIELDS_FILTER_SEARCH_DESC'); ?></label>
                    <input type="text" name="filter_search" placeholder="<?php echo JText::_('COM_AUTOGENERATOR_FIELDS_FILTER_SEARCH_DESC'); ?>" id="filter_search" value="<?php echo $this->escape($this->state->get('filter.search')); ?>" title="<?php echo JText::_('COM_AUTOGENERATOR_FIELDS_FILTER_SEARCH_DESC'); ?>" />
                </div>
                <div class="btn-group pull-left hidden-phone">
                    <button class="btn tip hasTooltip" type="submit" title="<?php echo JText::_('JSEARCH_FILTER_SUBMIT'); ?>"><i class="icon-search"></i></button>
                    <button class="btn tip hasTooltip" type="button" onclick="document.id('filter_search').value='';this.form.submit();" title="<?php echo JText::_('JSEARCH_FILTER_CLEAR'); ?>"><i class="icon-remove"></i></button>
                </div>
                <div class="btn-group pull-right hidden-phone">
                    <label for="limit" class="element-invisible"><?php echo JText::_('JFIELD_PLG_SEARCH_SEARCHLIMIT_DESC'); ?></label>
                    <?php echo $this->pagination->getLimitBox(); ?>
                </div>
                <div class="btn-group pull-right hidden-phone">
                    <label for="directionTable" class="element-invisible"><?php echo JText::_('JFIELD_ORDERING_DESC'); ?></label>
                    <select name="directionTable" id="directionTable" class="input-medium" onchange="Joomla.orderTable()">
                        <option value=""><?php echo JText::_('JFIELD_ORDERING_DESC'); ?></option>
                        <option value="asc" <?php if ($listDirn == 'asc') echo 'selected="selected"'; ?>><?php echo JText::_('JGLOBAL_ORDER_ASCENDING'); ?></option>
                        <option value="desc" <?php if ($listDirn == 'desc') echo 'selected="selected"'; ?>><?php echo JText::_('JGLOBAL_ORDER_DESCENDING'); ?></option>
                    </select>
                </div>
                <div class="btn-group pull-right">
                    <label for="sortTable" class="element-invisible"><?php echo JText::_('JGLOBAL_SORT_BY'); ?></label>
                    <select name="sortTable" id="sortTable" class="input-medium" onchange="Joomla.orderTable()">
                        <option value=""><?php echo JText::_('JGLOBAL_SORT_BY'); ?></option>
                        <?php echo JHtml::_('select.options', $this->getSortFields(), 'value', 'text', $listOrder); ?>
                    </select>
                </div>
            </div>
            <div class="clearfix"> </div>

            <table class="table table-striped" id="fieldsList">
                <thead>
                    <tr>
                        <th width="1%" class="nowrap center hidden-phone">
                            <?php echo JHtml::_('grid.sort', '<i class="icon-menu-2"></i>', 'af.ordering', $listDirn, $listOrder, null, 'asc', 'COM_AUTOGENERATOR_FIELDS_HEADER_ORDERING'); ?>
                        </th>
                        <th width="1%" class="hidden-phone">
                            <input type="checkbox" name="checkall-toggle" value="" title="<?php echo JText::_('JGLOBAL_CHECK_ALL'); ?>" onclick="Joomla.checkAll(this)" />
                        </th>
                        <!-- START LIST OF HEADER -->
                        
                        <th>
                            <?php echo JHtml::_('grid.sort', 'COM_AUTOGENERATOR_FIELDS_HEADER_SQL_FIELD', 'af.sql_field', $listDirn, $listOrder); ?>
                        </th>
                        <th width="25%" >
                            <?php echo JHtml::_('grid.sort', 'COM_AUTOGENERATOR_FIELDS_HEADER_XML_TYPE', 'af.xml_type_id', $listDirn, $listOrder); ?>
                        </th>
                        <th width="15%" >
                            <?php echo JHtml::_('grid.sort', 'COM_AUTOGENERATOR_FIELDS_HEADER_TABLE_NAME', 'at.title', $listDirn, $listOrder); ?>
                        </th>
                        <th style="display: none;" >
                            <?php echo JHtml::_('grid.sort', 'COM_AUTOGENERATOR_FIELDS_HEADER_SQL_TYPE', 'af.sql_type', $listDirn, $listOrder); ?>
                        </th>
                        <th style="display: none;" >
                            <?php echo JHtml::_('grid.sort', 'COM_AUTOGENERATOR_FIELDS_HEADER_SQL_NULL', 'af.sql_null', $listDirn, $listOrder); ?>
                        </th>
                        <th style="display: none;" >
                            <?php echo JHtml::_('grid.sort', 'COM_AUTOGENERATOR_FIELDS_HEADER_SQL_KEY', 'af.sql_key', $listDirn, $listOrder); ?>
                        </th>
                        <th style="display: none;" >
                            <?php echo JHtml::_('grid.sort', 'COM_AUTOGENERATOR_FIELDS_HEADER_SQL_DEFAULT', 'af.sql_default', $listDirn, $listOrder); ?>
                        </th>
                        <th style="display: none;" >
                            <?php echo JHtml::_('grid.sort', 'COM_AUTOGENERATOR_FIELDS_HEADER_SQL_EXTRA', 'af.sql_extra', $listDirn, $listOrder); ?>
                        </th>
                        <th width="10%" class="nowrap hidden-phone center">
                            <?php echo JText::_('COM_AUTOGENERATOR_FIELDS_HEADER_FUNCTION'); ?>
                        </th>
                        <th width="10%" class="nowrap hidden-phone center">
                            <?php echo JHtml::_('grid.sort', 'COM_AUTOGENERATOR_FIELDS_HEADER_ALLOW_EDIT_IN_LIST', 'af.allow_edit_in_list', $listDirn, $listOrder); ?>
                        </th>
                        
                        <th width="10%" class="nowrap hidden-phone center">
                            <?php echo JText::_('COM_AUTOGENERATOR_FIELDS_HEADER_ORDERING_FIELD'); ?>
                        </th>
                        <th width="5%" class="nowrap hidden-phone center">
                            <?php echo JHtml::_('grid.sort', 'COM_AUTOGENERATOR_FIELDS_HEADER_STATUS', 'af.status', $listDirn, $listOrder); ?>
                        </th>
                        <th width="1%" class="nowrap hidden-phone">
                            <?php echo JHtml::_('grid.sort', 'COM_AUTOGENERATOR_FIELDS_HEADER_ID', 'af.id', $listDirn, $listOrder); ?>
                        </th>
                        <!-- END LIST OF HEADER -->
                    </tr>
                </thead>
                <tbody>
                    <?php if ($this->items !== false) : ?>
                        <?php
                        foreach ($this->items as $i => $item) :
                            $disableClassName = '';
                            $disabledLabel = '';
                            if (!$saveOrder) :
                                $disabledLabel = JText::_('JORDERINGDISABLED');
                                $disableClassName = 'inactive tip-top';
                            endif;
                            ?>
                            <tr class="row<?php echo $i % 2; ?>" sortable-group-id="<?php echo $item->table_id; ?>">
                                <td class="order nowrap center hidden-phone">
                                    <span class="sortable-handler hasTooltip <?php echo $disableClassName; ?>" title="<?php echo $disabledLabel; ?>">
                                        <i class="icon-menu"></i>
                                    </span>
                                    <input type="text" style="display:none" name="order[]" size="5" value="<?php echo $item->ordering; ?>" class="width-20 text-area-order " />
                                </td>
                                <td class="center hidden-phone">
                                    <?php echo JHtml::_('grid.id', $i, $item->id); ?>
                                </td>
                                                     
                                <td class="nowrap has-context">
                                    <div class="pull-left">
                                        <a href="<?php echo JRoute::_('index.php?option=com_autogenerator&task=field.edit&id=' . $item->id); ?>" title="<?php echo JText::_('JACTION_EDIT'); ?>">
                                            <?php echo $this->escape($item->sql_field); ?>
                                        </a>
                                    </div>
                                    <div class="pull-left">
                                        <?php
                                        // Create dropdown items
                                        JHtml::_('dropdown.edit', $item->id, 'field.');
                                        // Render dropdown list
                                        echo JHtml::_('dropdown.render');
                                        ?>
                                    </div>
                                </td>
                                <td class="small hidden-phone autogenerator-select-inline">
                                    <span class="autogenerator-select-inline-xml-type-title" title="<?php echo JText::_('COM_AUTOGENERATOR_XML_ELEMENTS_XML_TYPE_SELECT_CLICK_TO_SELECT_INLINE'); ?>">
                                        <?php echo $this->escape($item->xml_type_id == 0 ? JText::_('COM_AUTOGENERATOR_FIELDS_XML_TYPE_DEFAULT') : $item->xml_type_name); ?>
                                    </span>
                                    <span class="autogenerator-select-holder" style="display: none;">
                                        <input type="hidden" class="autogenerator-select-xml-type-id" name="selectInlineXmlTypeId[]" value="<?php echo $item->xml_type_id; ?>" />
                                        <input type="hidden" class="autogenerator-select-field-id" name="selectInlineFieldId[]" value="<?php echo $item->id; ?>" />

                                        <?php echo JHtml::_('select.genericlist', $this->aXMLTypeOptions, 'selectInline[]', 'class="autogenerator-select-list inputbox required" size="30"', 'value', 'text', $item->xml_type_id, 'select_inline_' . $item->id); ?>
                                        &nbsp;&nbsp;
                                        <a href="#" class="autogenerator-select-save">Save</a>
                                        &nbsp;&nbsp;
                                        <a href="#" class="autogenerator-select-cancel">Cancel</a>
                                    </span>
                                    <a href="<?php echo JRoute::_('index.php?option=com_autogenerator&task=field.editXmlTypeInline'); ?>" class="autogenerator-select-link">&nbsp;</a>
                                </td>
                                <td class="small hidden-phone">
                                    <?php echo $this->escape($item->table_name); ?>
                                </td>
                                <td class="small hidden-phone" style="display: none;" >
                                    <?php echo $this->escape($item->sql_type); ?>
                                </td>
                                <td class="small hidden-phone" style="display: none;" >
                                    <?php echo $this->escape($item->sql_null); ?>
                                </td>
                                <td class="small hidden-phone" style="display: none;" >
                                    <?php echo $this->escape($item->sql_key); ?>
                                </td>
                                <td class="small hidden-phone" style="display: none;" >
                                    <?php echo $this->escape($item->sql_default); ?>
                                </td>
                                <td class="small hidden-phone" style="display: none;" >
                                    <?php echo $this->escape($item->sql_extra); ?>
                                </td>
                                <td class="small hidden-phone center">
                                    <a href="<?php echo JRoute::_('index.php?option=com_autogenerator&task=field.edit&layout=check&id=' . $item->id); ?>">
                                        Review
                                    </a>
                                </td>
                                <td class="small hidden-phone center">
                                    <a class="btn btn-micro autogenerator-change-status" href="<?php echo JRoute::_('index.php?option=com_autogenerator&task=field.allowEditInList&iFieldId=' . $item->id); ?>" title="<?php echo ($item->allow_edit_in_list ? 'Enable' : 'Disable'); ?>">
                                        <i class="<?php echo ($item->allow_edit_in_list ? 'icon-publish' : 'icon-unpublish'); ?>"></i>
                                    </a>
                                </td>
                                <td class="small hidden-phone center">
                                    <a class="btn btn-micro autogenerator-change-status autogenerator-change-unique" href="<?php echo JRoute::_('index.php?option=com_autogenerator&task=field.setOrderingField&iFieldId=' . $item->id); ?>" title="<?php echo ($item->is_ordering_field ? 'Enable' : 'Disable'); ?>">
                                        <i class="<?php echo ($item->is_ordering_field ? 'icon-publish' : 'icon-unpublish'); ?>"></i>
                                    </a>
                                </td>
                                <td class="small hidden-phone center">
                                    <a class="btn btn-micro autogenerator-change-status" href="<?php echo JRoute::_('index.php?option=com_autogenerator&task=field.changeStatus&iFieldId=' . $item->id); ?>" title="<?php echo ($item->status ? 'Enable' : 'Disable'); ?>">
                                        <i class="<?php echo ($item->status ? 'icon-publish' : 'icon-unpublish'); ?>"></i>
                                    </a>
                                </td>
                                
                                <td class="center hidden-phone">
                                    <?php echo (int) $item->id; ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
            <?php echo $this->pagination->getListFooter(); ?>
            
            <input type="hidden" name="task" value="" />
            <input type="hidden" name="boxchecked" value="0" />
            <input type="hidden" name="filter_order" value="<?php echo $listOrder; ?>" />
            <input type="hidden" name="filter_order_Dir" value="<?php echo $listDirn; ?>" />
            <?php echo JHtml::_('form.token'); ?>
        </div>
</form>
